package com.huqu.beans;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.huqu.dao.N100Dao;
import com.huqu.dao.S100Dao;
import com.huqu.entities.S100;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;
import com.huqu.utils.LuceneControl;

public class LuceneDemo {
	public static final String FILES_TO_INDEX_DIRECTORY = "fileToIndex";
	public static final String INDEX_DIRECTORY = "indexDirectory";

	public static final String FIELD_PATH = "path";
	public static final String FIELD_CONTENTS = "contents";

	public static void main(String[] args) {
		System.out.println("--- DemoLucene ---");
		long beginTime = System.currentTimeMillis();
		//
		// createIndex2();
		// searchID(22);
		// searchKey("Quynh", 100);
		//searchKey("sss",1000);
		//getDetailUrl("http://ngoisao.net/");
		//getDetailUrl2("http://uit.edu.vn/");
		try {
			List<String> listurl =new ArrayList<String>();
			/*listurl.add("http://giaoduc.net.vn/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/");
			listurl.add("http://kenhtuyensinh.vn/tuyen-sinh");
			listurl.add("http://www.tuyensinhvn.com/");
			listurl.add("http://www.huongnghiepviet.com/v3/");
			listurl.add("http://www.huongnghiep24h.net/");*/
			/*listurl.add("http://huongnghiep.hoasen.edu.vn/content/nghe-nghiep-danh-cho-ban");
			listurl.add("http://huongnghiep24h.com/tu-van-nghe-nghiep/");
			listurl.add("http://vnexpress.net/nghe-nghiep/tag-49114.html");
			listurl.add("http://www.24h.com.vn/tuyen-sinh-dh-cd-c365.html");
			listurl.add("http://xahoi.com.vn/giao-duc-du-hoc/tuyen-sinh/");
			listurl.add("http://tin.tuyensinh247.com/");
			listurl.add("http://www.tinmoi.vn/C/Huong-nghiep");
			listurl.add("http://thongtintuyensinh.vn/Huong-nghiep-Chon-nghe_C218.htm");
			*/
			
			/*listurl.add("http://news.zing.vn/song-tre/giao-duc.html");
			listurl.add("http://news.zing.vn/tuyen-sinh-dai-hoc-cao-dang.html");

			listurl.add("http://dantri.com.vn/giao-duc-khuyen-hoc.htm");
			listurl.add("http://dantri.com.vn/giao-duc-khuyen-hoc/tin-tuyen-sinh/trang-1.htm");
			listurl.add("http://dantri.com.vn/giao-duc-khuyen-hoc/khuyen-hoc/trang-1.htm");
			listurl.add("http://dantri.com.vn/giao-duc-khuyen-hoc/nghe-hot/trang-1.htm");
			listurl.add("http://duhoc.dantri.com.vn/");

			listurl.add("http://www.24h.com.vn/giao-duc-du-hoc-c216.html");

			listurl.add("http://vnexpress.net/tin-tuc/giao-duc");
			listurl.add("http://vnexpress.net/tin-tuc/giao-duc/du-hoc");
			listurl.add("http://vnexpress.net/tin-tuc/giao-duc/tuyen-sinh");
			listurl.add("http://vnexpress.net/tin-tuc/giao-duc/tu-van");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/tuyen-sinh/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/chuyen-giang-duong/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/du-hoc/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/nep-nha/");
			listurl.add("http://vietnamnet.vn/vn/giao-duc/dien-dan/");

			listurl.add("http://giaoduc.net.vn/Giao-duc-24h/5.gd");
	
			listurl.add("http://www.doisongphapluat.com/giao-duc/");
			listurl.add("http://www.doisongphapluat.com/giao-duc/tuyen-sinh/");
			listurl.add("http://www.doisongphapluat.com/giao-duc/du-hoc/");
			listurl.add("http://www.doisongphapluat.com/giao-duc/chuyen-hoc-duong/");
			listurl.add("http://giaoduc.net.vn/Giao-duc-24h/5.gd");*/
			//org.jsoup.nodes.Document doc = Jsoup.connect("www.kh-sdh.udn.vn/zipfiles/so39q1/so39bai42.pdf").get();
			/*Connection.Response rs2 = Jsoup.connect("http://www.nfib.com/Portals/0/PDF/sbet/sbet201402.pdf")
			        .ignoreContentType(true)
			        .execute();
			System.out.println("Header = " + rs2.header("Last-Modified"));
			System.out.println(" url= " + rs2.url());
			System.out.println(" = " + rs2.charset());
			System.out.println(" = " + rs2.contentType());
			System.out.println(" = " + rs2.statusMessage());
			System.out.println(" = " + rs2.toString());
			System.out.println(" = " + rs2.parse());*/
			
			//System.out.println("body = " + rs2.body());
			/*if(doc!=null){
				System.out.println("title= "+doc.title());
				System.out.println("title= "+doc.text());
			}*/
			

			/*for(String str:listurl){
				org.jsoup.nodes.Document doc = Jsoup.connect(str).get();
				System.out.println("Fetching " + str + "...");

				if (doc != null) {
					Elements links = doc.select("a[href]");
					for (Element link : links) {
						LuceneControl.getDetailUrlToInsert(link.attr("abs:href"));
					}
				}
			}*/
			
			//createIndex2();
			
			//List<S100> listS100=LuceneControl.loadListS100();
			//LuceneControl.createIndex(listS100);
			
			/*List<ScoreDoc> listScore=LuceneControl.searchIndex("hướng nghiệp", 3000);
			System.out.println("hit = "+listScore.size());
			List<S100> listS100=LuceneControl.getResultSearch(listScore, 1, 100);
			System.out.println("result search = "+listS100.size());
			for(S100 s100:listS100){
				System.out.println("ps100="+s100.getPs100()+"-----"+s100.getSv101());
				System.out.println("Title: "+s100.getSv102());
				System.out.println("-------------------------------------------------");
			}*/
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		//LuceneControl.autoInsertFromUrl("http://huongnghiep.vn/");
		/*int i=("quynh_khung").indexOf("quynhss");
		System.out.println("-------------------"+i);*/
		//
		long endTime = System.currentTimeMillis();
		long time = (endTime - beginTime);
		double t = (double) time / 1000;
		System.out.println("---Search time:" + t + " s");
	}

	public static void searchID(int id) {
		try {
			Directory directory = FSDirectory.open(new File(
					"C://Users//CONGQUYNH//test"));
			IndexReader indexReader = DirectoryReader.open(directory);
			IndexSearcher indexSearcher = new IndexSearcher(indexReader);
			org.apache.lucene.document.Document document = indexSearcher
					.doc(id);
			System.out.println("searchID( " + id + " ): ");
			if (document != null) {
				System.out.println("id= " + document.get("id"));
				System.out.println("name= " + document.get("name"));
				System.out.println("age= " + document.get("age"));
				System.out.println("address= " + document.get("address"));
				System.out.println("ime= " + document.get("ime"));
			} else {
				System.out.println("Khong co ket qua.");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void getDetailUrl2(String url) {
		try {
			org.jsoup.nodes.Document doc = Jsoup.connect(url).get();
			System.out.println("Fetching " + url + "...");

			if (doc != null) {
				System.out.println("--------Ket noi thanh cong-----------");
				S100 s100 = new S100();
				Elements links = doc.select("a[href]");
				for (Element link : links) {
					String urlLink = link.attr("abs:href");
					if (urlLink != null && urlLink.length() > 0
							&& urlLink.indexOf("mailto:") < 0) {
						System.out.println("--- GetDetailUrl( " + urlLink
								+ " )");
						org.jsoup.nodes.Document doc_auto = Jsoup.connect(
								urlLink).get();

						if (doc_auto != null) {
							s100 = new S100();
							s100.setPs100(0);
							s100.setSv101(urlLink);
							s100.setSv102(doc_auto.title());
							s100.setSv103(doc_auto.text());
							s100.setSn107(0);
							s100.setSn108(3);

							S100Dao s100Dao = (S100Dao) ApplicationHelper
									.findBean(ApplicationConstant.REPOSITORY_NAME_S100);
							int result = s100Dao.InsertTabS100(s100.getPs100(),
									s100.getSv101(), s100.getSv102(),
									s100.getSv103(), s100.getSn107(),
									s100.getSn108());
							if (result > 0) {
								System.out.println("result= " + result);
							}
						}
					}
				}

			} else {
				System.err.println("--------Ket noi that bai-----------");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void getDetailUrl(String url) {
		try {
			org.jsoup.nodes.Document doc = Jsoup.connect(url).get();
			Elements links = doc.select("a[href]");
			Elements media = doc.select("[src]");
			Elements imports = doc.select("link[href]");
			String text = doc.text();
			System.out.println("Fetching " + url + "...");
			System.err.println(text);
			/*
			 * for (Element src : media) { if (src.tagName().equals("img")) {
			 * System.out.println("   * " + src.tagName() + "<" +
			 * src.attr("abs:src") + "> " + src.attr("width") + "x" +
			 * src.attr("height") + "---"+src.attr("alt")); } }
			 */

			/* lay url index */
			/*
			 * Element link = doc.select("a").first(); String absHref =
			 * link.attr("abs:href"); if (absHref != null && absHref.length() >
			 * 1){ System.out.println("absHref= "+ absHref); }
			 */

			for (Element link : links) {
				System.err.println("-----" + link.attr("abs:href"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void searchKey(String keySearch, int numrow) {
		try {
			System.out.println("searchKey( " + keySearch + " , " + numrow
					+ " ): ");
			Version version = Version.LUCENE_46;
			Directory directory = FSDirectory
					.open(new File(
							"E://Tai lieu hoc tap//DEV//workspace//huongnghiep//src//main//webapp//lucene"));
			IndexReader indexReader = DirectoryReader.open(directory);
			IndexSearcher indexSearcher = new IndexSearcher(indexReader);
			StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
			// Query query= new QueryParser(Version.LUCENE_46, "name",
			// analyzer).parse(keySearch);
			QueryParser querypaser = new QueryParser(version, null,
					new StandardAnalyzer(version));
			/*
			 * Query query = MultiFieldQueryParser.parse(Version.LUCENE_46, new
			 * String[] { "Ha", "Son" }, new String[] { "name", "address" }, new
			 * SimpleAnalyzer( Version.LUCENE_46));
			 */
			/* Query query = MultiFieldQueryParser.p */
			Query query = querypaser.parse("(sv102:zing.vn)^2(sv101:zing.vn)");
			//Query query = querypaser.parse("sv101:zing.vn or sv102:zing.vn");
			// query=MultiFieldQueryParser.par
			TopScoreDocCollector collector = TopScoreDocCollector.create(
					numrow, true);

			indexSearcher.search(query, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;

			System.out.println("Found " + hits.length + " hits.");
			System.out.println("id= " + "-----------------" + "name= "
					+ "-----------------" + "ime= ");
			for (int i = 0; i < hits.length; i++) {
				int docid = hits[i].doc;
				Document d = indexSearcher.doc(docid);
				if (d != null) {
					System.out
							.println(d.get("id") + "-----" + d.get("ps100")
									+ "----" + d.get("sv101") + "----"
									+ d.get("sv102"));
				} else {
					System.out.println("Khong co ket qua.");
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createIndex2() {
		try {
			// Create a File object with theFile name of the Lucene index and
			// open that index: "/home/ronnie/myIndex/"
			File file = new File("src//main//webapp//lucene");
			FSDirectory directory = FSDirectory.open(file);

			// Create an analyzer that will be used with the IndexWriter:
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);

			// Build an IndexWriterConfig to use as the configuration for the
			// indexWriter:
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
					Version.LUCENE_46, analyzer);
			indexWriterConfig
					.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

			// Build the indexWriter:
			IndexWriter writer = new IndexWriter(directory, indexWriterConfig);

			Random random = new Random();
			ArrayList<String> arr = new ArrayList<String>();
			arr.add("Nguyen Cong Nhat");
			arr.add("Nguyen Cong Ha");
			arr.add("Nguyen Cong Lam");
			arr.add("Nguyen Cong Quynh");
			arr.add("Nguyen Cong Trang");
			arr.add("Nguyen Cong Ngoc");
			arr.add("Nguyen Cong Xung");
			arr.add("Nguyen Cong Tuyen");
			arr.add("Nguyen Cong Tien");
			arr.add("Nguyen Cong Hai");
			arr.add("Nguyen Cong Canh");
			arr.add("Nguyen Cong Minh");

			ArrayList<String> arradd = new ArrayList<String>();
			arradd.add("Vu Cong");
			arradd.add("Vu Hoa");
			arradd.add("Quang Binh");
			arradd.add("Vu Qui");
			arradd.add("Vu Son");
			arradd.add("Vu Binh");
			arradd.add("Quang Lich");
			arradd.add("Vu Trung");
			arradd.add("Vu Tien");
			arradd.add("Vu Thang");
			arradd.add("Vu Ninh");
			arradd.add("Vu Lac");
			arradd.add("Vu Dong");
			arradd.add("Vu Tay");

			for (int i = 0; i < 100; i++) {
				Document doc = new Document();
				doc.add(new IntField("id", i + 1, Field.Store.YES));
				doc.add(new TextField("name", arr.get(random.nextInt(10)),
						Field.Store.YES));
				doc.add(new TextField("address",
						arradd.get(random.nextInt(13)), Field.Store.YES));
				doc.add(new IntField("age", random.nextInt(100),
						Field.Store.YES));
				doc.add(new IntField("ime", random.nextInt(10000),
						Field.Store.YES));
				writer.addDocument(doc);
			}

			writer.commit();
			writer.close();
			analyzer.close();
			directory.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * public static void searchIndex(String searchString) throws IOException,
	 * ParseException { System.out.println("Searching for '" + searchString +
	 * "'"); Directory directory = FSDirectory.open(new File(INDEX_DIRECTORY));
	 * IndexReader indexReader = IndexReader.open(directory); IndexSearcher
	 * indexSearcher = new IndexSearcher(indexReader);
	 * 
	 * Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46); QueryParser
	 * queryParser = new QueryParser(Version.LUCENE_46, FIELD_CONTENTS,
	 * analyzer); Query query = queryParser.parse(searchString); Hits hits =
	 * indexSearcher.search(query, 0); System.out.println("Number of hits: " +
	 * hits.length());
	 * 
	 * Iterator<Hit> it = hits.iterator(); while (it.hasNext()) { Hit hit =
	 * it.next(); Document document = hit.getDocument(); String path =
	 * document.get(FIELD_PATH); System.out.println("Hit: " + path); }
	 * 
	 * }
	 */
}
